<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\AudioApi;


class MoonDial extends Base
{
    protected $table = 'moon_dial_log';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    public function create($body = [], $rank = 1, $date = '')
    {
        $data['user_id'] = $body['user_id'] ?? 0;
        $data['gift_type'] = $body['gift_type'] ?? 0;
        $data['gift_value'] = $body['gift_value'] ?? 1;
        $data['num_count'] = $body['num_count'] ?? 1;
        return DB::table($this->table)->insert($data);
    }

    public function getNumToLiang($user_id)
    {
        $where['user_id'] = $user_id;
        $where['gift_type'] = 4;
        $res = DB::table($this->table)->where($where)->first();
        if (!$res) {
            return false;
        }
        return true;
    }


    public function incrementNum($user_id, $num = 0)
    {
        $where['user_id'] = $user_id;
        return DB::table($this->table)->where($where)->increment('ticket_num', $num);
    }

    public function decrementNum($user_id, $num = 0)
    {
        $where['user_id'] = $user_id;
        DB::table($this->table)->where($where)->increment('consumer_num', $num);
        return DB::table($this->table)->where($where)->decrement('ticket_num', $num);
    }

    public function getAllTicket()
    {
        $data['ticket_sum'] =  DB::table($this->table)->sum('ticket_num') ?? 0;
        $data['consumer_sum'] =  DB::table($this->table)->sum('consumer_num') ?? 0;
        return $data;
    }

    public function getLog($user_id)
    {
        return DB::table($this->table)->where(['user_id'=>$user_id])->orderBy('id','desc')->get()->toArray();
    }

}